import {Reducer, Effect} from 'umi'

import {getCourseByPage, deleteCourse, addCourse, updateCourse, getComments, publishComment, deleteComment, markSubmit, applySysCourse, disApplySysCourse} from '../../services/course'
import {message} from "antd";

interface CourseModelType {
  namespace: string;
  state: {
    courseList: any[],
    commentList: any[],
    scoreMap: any[],
    applied: any[],
    total: Number,
  };
  reducers: {
    getCourseList: Reducer,
    getCommentList: Reducer,
  };
  effects: {
    getCourseByPage: Effect,
    deleteCourse: Effect,
    addCourse: Effect,
    updateCourse: Effect,
    getComments: Effect,
    publishComment: Effect,
    deleteComment: Effect,
    markSubmit: Effect,
    applySysCourse: Effect,
    disApplySysCourse: Effect,
  };
}

const CourseModel: CourseModelType = {
  namespace: "course",
  state: {
    courseList: [],
    commentList: [],
    scoreMap: [],
    applied: [],
    total: 0,
  },
  reducers: {
    getCourseList(state, {payload}){
      return ({
        ...state,
        courseList: payload.list.list,
        scoreMap: payload.scoreMap,
        total: payload.list.total,
        applied: payload.applied,
      })
    },
    getCommentList(state, {payload}){
      return ({
        ...state,
        commentList: payload,
      })
    },
  },
  effects: {
    *getCourseByPage({payload}, {put, call}) {
      const {data} = yield call(getCourseByPage, payload)
      yield put({
        type: 'getCourseList',
        payload: data,
      })
    },
    *deleteCourse({payload}, {call}) {
      const {code} = yield call(deleteCourse, payload)
      if(code === 200) {
        message.success("删除成功")
      } else {
        message.error("System Error")
      }
    },
    *addCourse({payload}, {call}) {
      const {code} = yield call(addCourse, payload)
      if(code === 200) {
        message.success("添加成功")
      } else {
        message.error("System Error")
      }
    },
    *updateCourse({payload}, {call}) {
      const {code} = yield call(updateCourse, payload)
      if(code === 200) {
        message.success("修改成功")
      } else {
        message.error("System Error")
      }
    },
    *getComments({payload}, {put, call}) {
      const {data} = yield call(getComments, payload)
      yield put({
        type: 'getCommentList',
        payload: data,
      })
    },
    *publishComment({payload}, {put, call}) {
      const {code} = yield call(publishComment, payload)
      if(code === 200) {
        message.success("发布成功")
      } else {
        message.error("System Error")
      }
    },
    *deleteComment({payload}, {put, call}) {
      const {code} = yield call(deleteComment, payload)
      if(code === 200) {
        message.success("删除成功")
      } else {
        message.error("System Error")
      }
    },
    *markSubmit({payload}, {put, call}) {
      const {code} = yield call(markSubmit, payload)
      if(code === 200) {
        message.success("评分成功")
      } else {
        message.error("System Error")
      }
    },
    *applySysCourse({payload}, {put, call}) {
      const {code} = yield call(applySysCourse, payload)
      if(code === 200) {
        message.success("报名成功")
      } else {
        message.error("System Error")
      }
    },
    *disApplySysCourse({payload}, {put, call}) {
      const {code} = yield call(disApplySysCourse, payload)
      if(code === 200) {
        message.success("取消报名成功")
      } else {
        message.error("System Error")
      }
    },
  },
}

export default CourseModel
